﻿using System.Collections.Generic;
using System.Xml;

namespace Assets.AnimationPlayer.Scripts.Animations
{
    public class AnimationObj
    {
        public string Id { get; set; }
        public string ImageUrl { get; set; }
        public string AnimationName { get; set; }
        public string AnimationPath { get; set; }

        public List<Angle> Angles { get; set; }

        public AnimationObj()
        {

        }

        public static List<AnimationObj> Get(XmlElement root)
        {
            List<AnimationObj> listObj = new List<AnimationObj>();

            XmlNode animationObjsNode = root.SelectSingleNode("AnimationObjs");
            if (animationObjsNode != null && animationObjsNode.HasChildNodes)
            {
                XmlNodeList animationObjs = animationObjsNode.ChildNodes;
                foreach (XmlNode skey in animationObjs)
                {
                    var obj = new AnimationObj
                    {
                        Id = skey["Id"] == null ? "-1" : skey["Id"].InnerText,
                        ImageUrl = skey["ImageUrl"] == null ? "" : skey["ImageUrl"].InnerText,
                        AnimationPath = skey["AnimationPath"] == null ? "" : skey["AnimationPath"].InnerText,
                        AnimationName = skey["AnimationName"] == null ? "" : skey["AnimationName"].InnerText,
                        Angles =  Angle.Get((XmlElement)skey)
                    };
                    listObj.Add(obj);
                }
            }
            return listObj;
        }
    }

    public class Angle
    {
        public string CameraX { get; set; }
        public string CameraY { get; set; }
        public string CameraZ { get; set; }
        public string CameraRoateX { get; set; }
        public string CameraRoateY { get; set; }
        public string CameraRoateZ { get; set; }
        public string CameraFieldView { get; set; }
        public string CameraNearClipPlane { get; set; }
        public string CameraFarClipPlane { get; set; }

        public string RoateX { get; set; }
        public string RoateY { get; set; }
        public string RoateZ { get; set; }
        public string Scale { get; set; }
        public string Flip { get; set; }
        public string X { get; set; }
        public string Y { get; set; }
        public string Z { get; set; }

        public string FlipX { get; set; }
        public string FlipY { get; set; }
        public string FlipZ { get; set; }

        public Angle()
        {
            CameraX = "0";
            CameraY = "0";
            CameraZ = "0";
            CameraRoateX = "0";
            CameraRoateY = "0";
            CameraRoateZ = "0";
            CameraFieldView = "2";
            CameraNearClipPlane = "0.3";
            CameraFarClipPlane = "1000";

            RoateX = "0";
            RoateY = "0";
            RoateZ = "0";
            Scale = "1";
            Flip = "true";
            X = "0";
            Y = "0";
            Z = "0";

            FlipX = "false";
            FlipY = "false";
            FlipZ = "false";
        }

        public static List<Angle> Get(XmlElement root)
        {
            List<Angle> listObj = new List<Angle>();

            XmlNode animationObjsNode = root.SelectSingleNode("Angles");
            if (animationObjsNode != null && animationObjsNode.HasChildNodes)
            {
                XmlNodeList animationObjs = animationObjsNode.ChildNodes;
                foreach (XmlNode skey in animationObjs)
                {
                    var obj = new Angle
                    {
                        CameraX = skey["CameraX"] == null ? "0" : skey["CameraX"].InnerText,
                        CameraY = skey["CameraY"] == null ? "0" : skey["CameraY"].InnerText,
                        CameraZ = skey["CameraZ"] == null ? "0" : skey["CameraZ"].InnerText,
                        CameraRoateX = skey["CameraRoateX"] == null ? "0" : skey["CameraRoateX"].InnerText,
                        CameraRoateY = skey["CameraRoateY"] == null ? "0" : skey["CameraRoateY"].InnerText,
                        CameraRoateZ = skey["CameraRoateZ"] == null ? "0" : skey["CameraRoateZ"].InnerText,
                        CameraFieldView = skey["CameraFieldView"] == null ? "2" : skey["CameraFieldView"].InnerText,
                        CameraNearClipPlane = skey["CameraNearClipPlane"] == null ? "0.3" : skey["CameraNearClipPlane"].InnerText,
                        CameraFarClipPlane= skey["CameraFarClipPlane"] == null ? "1000" : skey["CameraFarClipPlane"].InnerText,

                        RoateX = skey["RoateX"] == null ? "0" : skey["RoateX"].InnerText,
                        RoateY = skey["RoateY"] == null ? "0" : skey["RoateY"].InnerText,
                        RoateZ = skey["RoateZ"] == null ? "0" : skey["RoateZ"].InnerText,
                        Scale = skey["Scale"] == null ? "1" : skey["Scale"].InnerText,
                        X = skey["X"] == null ? "0" : skey["X"].InnerText,
                        Y = skey["Y"] == null ? "0" : skey["Y"].InnerText,
                        Z = skey["Z"] == null ? "0" : skey["Z"].InnerText,
                        FlipX = skey["FlipX"] == null ? "flase" : skey["FlipX"].InnerText,
                        FlipY = skey["FlipY"] == null ? "flase" : skey["FlipY"].InnerText,
                        FlipZ = skey["FlipZ"] == null ? "flase" : skey["FlipZ"].InnerText,
                        Flip = skey["Flip"] == null ? "true" : skey["Flip"].InnerText
                    };
                    listObj.Add(obj);
                }
            }
            return listObj;
        }
    }
}
